<?php

namespace app\api\logic;

use app\admin\model\user\UserMessage;
use app\basic\BaseLogic;

class UserMessageLogic extends BaseLogic
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new UserMessage();
    }

    /**
     * 获取消息列表
     * @param int $userId 用户ID
     * @param int $type 消息类型，0为全部
     * @param int $page 页码
     * @param int $limit 每页数量
     * @return array
     */
    public function getMessageList($userId, $type = 0, $page = 1, $limit = 10)
    {
        $query = $this->model->where('user_id', $userId);
        
        // 按类型筛选
        if ($type > 0) {
            $query->where('type', $type);
        }

        $total = $query->count();
        
        $list = $query->field('id, type, title, content, status, create_time')
            ->order('create_time', 'desc')
            ->page($page, $limit)
            ->select()
            ->each(function($item) {
                if (is_numeric($item['create_time'])) {
                    $item['create_time'] = date('Y-m-d H:i:s', (int)$item['create_time']);
                }
                return $item;
            })
            ->toArray();

        return [
            'list' => $list,
            'total' => $total,
            'page' => (int)$page,
            'limit' => (int)$limit
        ];
    }

    /**
     * 获取消息统计
     * @param int $userId 用户ID
     * @return array
     */
    public function getMessageCount($userId)
    {
        // 获取未读消息数量
        $unreadCount = $this->model->where('user_id', $userId)
            ->where('status', 0)
            ->count();

        // 获取总消息数量
        $totalCount = $this->model->where('user_id', $userId)
            ->count();

        return [
            'unread_count' => $unreadCount,
            'total_count' => $totalCount
        ];
    }

    /**
     * 标记消息为已读
     * @param int $userId 用户ID
     * @param string $ids 消息ID，多个以逗号分隔
     * @return bool
     */
    public function markAsRead($userId, $ids)
    {
        $idArray = array_filter(explode(',', $ids), function($id) {
            return is_numeric($id) && $id > 0;
        });

        if (empty($idArray)) {
            return false;
        }
        return $this->model->where('user_id', $userId)
            ->whereIn('id', $idArray)
            ->update(['status' => 1]);
    }

    /**
     * 标记所有消息为已读
     * @param int $userId 用户ID
     * @return bool
     */
    public function markAllAsRead($userId)
    {
        return $this->model->where('user_id', $userId)
            ->where('status', 0)
            ->update(['status' => 1]);
    }

    /**
     * 删除消息
     * @param int $userId 用户ID
     * @param string $ids 消息ID，多个以逗号分隔
     * @return bool
     */
    public function deleteMessage($userId, $ids)
    {
        $idArray = array_filter(explode(',', $ids), function($id) {
            return is_numeric($id) && $id > 0;
        });

        if (empty($idArray)) {
            return false;
        }

        return $this->model->where('user_id', $userId)
            ->whereIn('id', $idArray)
            ->delete();
    }
} 